package com.yami.shop.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yami.shop.bean.app.dto.CouponOrderDto;
import com.yami.shop.bean.dto.TtlCoupon;
import com.yami.shop.bean.model.Coupon;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface CouponMapper extends BaseMapper<Coupon> {

    /**
     * 获取用户没有领取的所有券包
     * @param userId 用户id
     * @return 用户没有领取的券包
     */
    @Select("SELECT * FROM tz_coupon WHERE coupon_id NOT IN (SELECT coupon_id FROM tz_user_coupon WHERE user_id = #{userId})")
    List<Coupon> selectValCoupon(String userId);

    /**
     * 获取用户拥有的所有券
     * @param userId 用户id
     * @return 用户拥有的券包
     */
    @Select("SELECT * FROM tz_coupon WHERE coupon_id IN (SELECT coupon_id FROM tz_user_coupon WHERE user_id = #{userId})")
    List<Coupon> selectUserCoupon(String userId);

    @Select("SELECT a.coupon_id, a.value, b.start_date, b.end_date, a.min_thres " +
            "FROM tz_coupon a JOIN tz_user_coupon b ON a.coupon_id = b.coupon_id " +
            "WHERE b.user_id = #{userId};")
    List<TtlCoupon> selectTtlCoupon(String userId);

    @Select("SELECT a.coupon_id, a.value, b.start_date, b.end_date, a.min_thres, b.status " +
            "FROM tz_coupon a JOIN tz_user_coupon b ON a.coupon_id = b.coupon_id " +
            "WHERE b.user_id = #{userId};")
    List<CouponOrderDto> selectCouponOrder(String userId);

    @Select("SELECT a.coupon_id, a.value, b.start_date, b.end_date, a.min_thres, b.status " +
            "FROM tz_coupon a JOIN tz_user_coupon b ON a.coupon_id = b.coupon_id WHERE a.coupon_id = #{couponId}" )
    CouponOrderDto selectCouponOrderByCouponId(Long couponId);

    @Select("SELECT * FROM tz_coupon WHERE coupon_id = #{couponId}")
    Coupon selectCouponByCouponId(Long couponId);



    @Update("UPDATE tz_user_coupon Set `status` = #{status} WHERE coupon_id = #{couponId}")
    void updateCouponStatus(Long couponId, Boolean status);



}
